package com.cheng.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: cheng
 * Date: 2014/5/5
 * Time: 18:27
 */
public class ZookeeperTest {
    private static final String zkUrl = "test-zk-1.qiyi.virtual:2181,test-zk-2.qiyi.virtual:2181,test-zk-3.qiyi.virtual:2181,test-zk-4.qiyi.virtual:2181,test-zk-5.qiyi.virtual:2181";
    private static final String user = "lego";
    private static final String pass = "lego";

    private static void createZkConnection() throws Exception {
//        PropertyConfigurator.configure(" D:/Code/conf/log4j.properties ");
        final Logger log = LoggerFactory.getLogger("test");
        // 创建一个与服务器的连接
        ZooKeeper zk = new ZooKeeper(zkUrl, 120000, new Watcher() {
            // 监控所有被触发的事件
            public void process(WatchedEvent event) {
                System.out.println("2:" + System.nanoTime());
                System.out.println("已经触发了" + event.getType() + "事件！");
                log.info("已经触发了" + event.getType() + "事件！");
            }
        });
        zk.addAuthInfo("digest", (user+":"+pass).getBytes());
        System.out.println("1:" + System.nanoTime());
//        // 创建一个目录节点
//        zk.create("/testRootPath", "testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
//                  CreateMode.PERSISTENT);
//        // 创建一个子目录节点
//        zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),
//                  ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//        System.out.println(new String(zk.getData("/testRootPath",false,null)));
//        // 取出子目录节点列表
//        System.out.println(zk.getChildren("/testRootPath",true));
//        // 修改子目录节点数据
//        zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);
//        System.out.println("目录节点状态：["+zk.exists("/testRootPath",true)+"]");
//        // 创建另外一个子目录节点
//        zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(),
//                  ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//        System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo",true,null)));
//        // 删除子目录节点
//        zk.delete("/testRootPath/testChildPathTwo",-1);
//        zk.delete("/testRootPath/testChildPathOne",-1);
//        // 删除父目录节点
//        zk.delete("/testRootPath",-1);
//        // 关闭连接
//        zk.close();
//        System.out.println(zk.getChildren("/lego/cms/chengen", true));
//        zk.create("/lego/cms/chengen/test","test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
//                  CreateMode.EPHEMERAL_SEQUENTIAL);
//        zk.create("/lego/cms/chengen/test","test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
//                  CreateMode.EPHEMERAL_SEQUENTIAL);
        while(true){
            Thread.sleep(1);
        }
    }

    public static void main(String[] args) {
        try {
            createZkConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
